<?php 
//-----------------------------------------------------------
//     NumbWordter - Number to Word converter class in PHP
//   Written by Manda Krishna Srikanth (krishnasrikanth.com)
//-----------------------------------------------------------
class numtoword
{
	private $zero = array('0'=>'Zero');
	
	private $discrete=array('1'=>'One',			'2'=>'Two',			'3'=>"Three",		'4'=>"Four",		'5'=>"Five",
	                        '6'=>'Six',			'7'=>'Seven',		'8'=>'Eight',		'9'=>'Nine',		'10'=>'Ten',
							'11'=>'Eleven',		'12'=>'Twelve',		'13'=>'Thirteen',	'14'=>'Fourteen',	'15'=>'Fifteen',
							'16'=>'sixteen',	'17'=>'seventeen',	'18'=>'eighteen',	'19'=>'nineteen',	'-'=>'minus', 
							'01'=>'One',		'02'=>'Two',		'03'=>"Three",		'04'=>"Four",		'05'=>"Five",
							'06'=>'Six',		'07'=>'Seven',		'08'=>'Eight',		'09'=>'Nine', 		'000'=>'', 
							'001'=>'One', 		'002'=>'Two',		'003'=>"Three",		'004'=>"Four",		'005'=>"Five",
							'006'=>'Six',		'007'=>'Seven',		'008'=>'Eight',		'009'=>'Nine');
	
	private $ten_digit_prefix=array('2'=>'Twenty',	'3'=>'Thirty',	'4'=>'Forty',	'5'=>'Fifty',
									'6'=>'Sixty',	'7'=>'Seventy',	'8'=>'Eighty',	'9'=>'Ninty');
	
	private $mool_array=array('',"Thousand","Million","Billion","Trillion","Quadrillion","Quintillion",
								"Sextillion","Septillion","Octillion","Nonillion","Decillion",
								"Unidecillion","Duodecillion","Tredecillion","Quattuordecillion");

	private $sentence; //final sentence
	private $error; //error if generated

	//methods
	private function twodigits($num)
	{
		//displays from 1 to 99
		if($num==0)
		{
			return $this->zero[$num];
		}elseif($num<20)
		{
			return $this->discrete[$num];
		}
		elseif($num==20 or $num==30 or $num==40 or $num==50 or $num==60 or $num==70 or $num==80 or $num==90)
		{
			return $this->ten_digit_prefix[substr($num,0,1)];
		}
		else
		{
			return $this->ten_digit_prefix[substr($num,0,1)].' '.$this->discrete[substr($num,1,1)];
		}
	}
	
	//displays three digit numbers
	private function threedigits($num,$count)
	{
		if($num==000)
		{
			return $this->discrete[000];
		}elseif($num==100 or $num==200 or $num==300 or $num==400 or $num==500 or $num==600 or $num==700 or $num==800 or $num==900)
		{
			if($count==0){
				return $this->discrete[substr($num,0,1)].' Hundred';
			}else{
				return $this->discrete[000].' and '.$this->discrete[substr($num,0,1)].' Hundred';
			}
			
		}elseif($num==001 or $num==002 or $num==003 or $num==004 or $num==005 or $num==006 or $num==007 or $num==008 or $num==009)
		{
			if($count==0){
				return $this->discrete[$num];
			}else{
				return $this->discrete[000].' and '.$this->discrete[$num];
			}
			
		}elseif(substr($num,0,1)=='0')
		{
			if($count==0){
				return $this->twodigits(substr($num,1,2));
			}else{
				return $this->discrete[000].' and '.$this->twodigits(substr($num,1,2));
			}
			
		}else
		{
			if($count==0){
				return $this->discrete[substr($num,0,1)].' Hundred and '.$this->twodigits(substr($num,1,2));
			}else{
				return $this->discrete[000].' and '.$this->discrete[substr($num,0,1)].' Hundred and '.$this->twodigits(substr($num,1,2));
			}
		}
	}
	
	private function decider($num,$count)
	{
		if(strlen($num)<=2)
			return $this->twodigits($num,$count);
		else
			return $this->threedigits($num,$count);
	}
	
	//comma
	private function comma($num,$i)
	{		
		if((substr($num,$i,1))==0)
		{
			return $this->zero[substr($num,$i,1)];
		}else
		{
			return $this->discrete[substr($num,$i,1)].' ';
		}
	}
	
	
	public function en($num)
	{
		$num=explode('.',$num);
		$numx=$num[0];
		$numy=$num[1];
		//return if more than 48 digits
		if(strlen($numx)>48) 
		{
			$this->error="Number out of bounds";
			return $this->error;
		}
		
		//check if first 
		if(substr($numx,0,1)=="-")
		{
			$this->sentence.='Minus ';
			$numx=substr($numx,1,strlen($numx)-1);
		}
		
		if(strlen($numx)<=3)
		{
				$this->sentence.=$this->decider($numx,0);
				//comma
				if($numy=='')
				{
					$this->sentence=$this->sentence.'';
				}elseif(array_sum(str_split($numy))==0)
				{
					$this->sentence=$this->sentence.'';
				}else
				{
					$this->sentence=$this->sentence.' Point ';
					for ($i=0;$i<(strlen($numy));$i++)
						{
							$this->sentence=$this->sentence.' '.$this->comma($numy,$i);
						}
				}
				return ucfirst(trim($this->sentence));
		}
		else
		{
			$k=strrev($numx);
			for($i=0;$i<strlen($k);$i=$i+3){$arro[]=strrev(substr($k,$i,3));}
			//reverse again
			$arro=array_reverse($arro);
			//print_r($arro);
			$mool=ceil(strlen($numx)/3);
			if((strlen(numx)%3)==0)
			$i==1;
			{$mool--;}
			$this->sentence.=$this->decider(($arro[0]),0).' '.$this->mool_array[$mool];
			$mool--;
			//leave the first one and prepare string of others
			$arrlen=count($arro);
			for($i=1;$i<$arrlen;$i++)
			{
				$this->sentence.=' '.$this->decider($arro[$i],1);
				if($mool!=0)
				{
					if($arro[$i]!='000')
					{
						$this->sentence=' '.$this->sentence.' '.$this->mool_array[$mool];
					}
				}
				$mool--;
			}
			//Comma
			if($numy=='')
			{
				$this->sentence=$this->sentence.'';
			}elseif(array_sum(str_split($numy))==0)
			{
				$this->sentence=$this->sentence.'';
			}
			else
			{
				$this->sentence=$this->sentence.' Point ';
				for ($i=0;$i<(strlen($numy));$i++)
					{
						$this->sentence=$this->sentence.$this->comma($numy,$i);
					}
			}
		}
		return ucfirst(trim($this->sentence));
	}
}
?>